﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Event_SG_EWDT
{
    public partial class Profile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Member"] == null)
            {
                Session["flag"] = "redirecterror";
                Response.Redirect("~/registration.aspx");
            }
            Member m = new Member();
            
            if (!IsPostBack)
            {

                m = MemberControls.RetrieveMember(Session["Member"].ToString());  // I dont know if this works man !!
                tbxProfileName.Text = m.Name;
                tbxProfileMobileNo.Text = m.Handphone;
            }

            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (m.interest % Convert.ToInt32( CheckBoxList1.Items[i].Value )== 0)
                {
                    CheckBoxList1.Items[i].Selected = true;
                }
            }

            //TabP  
            if (!IsPostBack)
            {
                TabContainerProfile.ActiveTabIndex = 0;
            }

            lblCheckPassword.Visible = false;
        }

        protected void btnProfileInfoUpdate_Click(object sender, EventArgs e)
        {
            int interest = 1;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    interest *= Convert.ToInt32(CheckBoxList1.Items[i].Value);
                }
            }
            
            bool b = MemberControls.UpdateMember(Session["Member"].ToString(), tbxProfileName.Text, tbxProfileMobileNo.Text, interest);

            if (b)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! Your profile has been updated!');", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps! One or more required fields were invalid!');", true);
            }
        }

        protected void gvMyEvent_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id = gvMyEvent.SelectedRow.Cells[0].Text;
            Response.Redirect("~/ViewEvent.aspx?eventid=" + id);
        }

        protected void btnProfilePasswordUpdate0_Click(object sender, EventArgs e)
        {
            // I assume the password is validated !!
            string enteredPass = Utility.GenerateHashFromPassword(tbxProfileCurrentPassword0.Text);
            string savedPass = MemberControls.RetrieveMemberPassword(Session["Member"].ToString());

            if (enteredPass == savedPass)
            {
                string newhashedpassword = Utility.GenerateHashFromPassword(tbxProfileNewPassword0.Text);

                bool b = MemberControls.UpdatePassword(Session["Member"].ToString(), newhashedpassword);

                if (b)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showSuccessToast', 'Yay! Your password change been changed!');", true);
                }
            }
            else
            {
                //put toast message here
                lblCheckPassword.Visible = true;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps! The password is not matched!');", true);

            }
        }

        protected void gvMyGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id = gvMyGroup.SelectedRow.Cells[0].Text;
            Response.Redirect("~/ViewGroup.aspx?groupid=" + id);
        }

        protected void gvPastEvent_SelectedIndexChanged(object sender, EventArgs e)
        {
            string id = gvPastEvent.SelectedRow.Cells[0].Text;
            Response.Redirect("~/ViewEvent.aspx?eventid=" + id);
        }

        protected void tbxProfileCurrentPassword0_TextChanged(object sender, EventArgs e)
        {
            lblCheckPassword.Visible = false;
        }

        
    }
}